// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  if (event.action == 'add') {
    return await cloud.database().collection('medicineInfo').add({
      data:{
        medicineDossage:event.medicineDossage,
        medicineId:event.medicineId,
        medicineLot:event.medicineLot,
        medicineName:event.medicineName,
        medicineNote:event.medicineNote,
        medicinePrice:event.medicinePrice,
        medicineSum:event.medicineSum,
        medicineUnit:event.medicineUnit,
        medicineUnitBig:event.medicineUnitBig,
      }
    }).then(res => {
      return res
    }).catch(res => {
      return res
    })
  }        
  else if(event.action=="del"){
    return await cloud.database().collection('medicineInfo').where({
      medicineId:event.medicineId
    }).remove()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="change"){
    return await cloud.database().collection('medicineInfo').where({
      medicineId:event.medicineId
    }).update({
      data:{
        medicineDossage:event.medicineDossage,
        medicineLot:event.medicineLot,
        medicineName:event.medicineName,
        medicineNote:event.medicineNote,
        medicinePrice:event.medicinePrice,
        medicineSum:event.medicineSum,
        medicineUnit:event.medicineUnit,
        medicineUnitBig:event.medicineUnitBig,
      }
    })
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="changeSum"){
    return await cloud.database().collection('medicineInfo').where({
      medicineId:event.medicineId
    }).update({
      data:{
        medicineSum:event.medicineSum,
      }
    })
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="find"){
    return await cloud.database().collection('medicineInfo').get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findOne"){
    return await cloud.database().collection('medicineInfo').where({
      medicineId:event.medicineId
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  
}

